package leetcode_core.leetcode_1;

import org.junit.Test;

import java.util.ArrayList;

public class MaxCoinsByBackTrack {
    private int res = Integer.MIN_VALUE;
    public int maxCoins(int[] nums) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int num : nums) {
            list.add(num);
        }//对数值赋值一份
        backTrack(list,0);
        return res;
    }


    private void backTrack(ArrayList<Integer> list, int score){
        if(list.isEmpty()){
            res = Math.max(res,score);
            return;
        }
        for(int i = 0;i<list.size();i++){
            int temp = list.get(i);//记忆起来,防止忘记
            int tempCoin = list.get(i)*( i-1<0 ? 1: list.get(i-1)) * (i+1 >= list.size() ?1 : list.get(i+1));
            //做选择
            list.remove(i);//选择了第i个,然后就把第i个给删除掉
            backTrack(list,score + tempCoin);
            //撤销选择
            list.add(i,temp);
        }
    }


    @Test
    public void testcase1(){
        int[] case1 = new int[]{3,1,5,8};
        System.out.println(maxCoins(case1));
    }

    @Test
    public void testcase2(){
        int[] case2 = new int[]{1,5};
        System.out.println(maxCoins(case2));
    }
}
